﻿using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System.Collections.Generic;

public class InputManager : MonoBehaviour , 
                                                IPointerEnterHandler, 
                                                IPointerExitHandler, 
                                                IPointerDownHandler, 
                                                IPointerUpHandler, 
                                                IPointerClickHandler,
                                                IBeginDragHandler,
                                                IDragHandler,
                                                IEndDragHandler,
                                                IDropHandler,
                                                IScrollHandler
{
    private void Start()
    {

    }

    public virtual void OnPointerEnter(PointerEventData eventData)
    {
    }

    public virtual void OnPointerExit(PointerEventData eventData)
    {
    }

    public Camera _Camera = null;

    RaycastHit2D[] hits = null;

    GameObject _SelectObj = null;

    public virtual void OnPointerDown(PointerEventData eventData)
    {
        //  画刷状态
        if (!string.IsNullOrEmpty(ZoneManager.Instance.floor))
        {
            ZoneManager.Instance._InputFloorComponent.OnPointerDown(eventData);

            return;
        }
        
        if (IsVirtual())
        {
            _SelectObj = ZoneManager.mVirualObject._Object;
        }
        else
        {
            hits = Physics2D.RaycastAll(_Camera.ScreenToWorldPoint(Input.mousePosition), _Camera.transform.forward, 1000);

            _SelectObj = GetTarget(hits);
        }

        if (_SelectObj != null)
        {
            ZoneManager.Instance.Seleted(_SelectObj);
        }
    }

    bool IsVirtual()
    {
        if (ZoneManager.mVirualObject != null)
            return true;

        return false;
    }

    public virtual void OnPointerUp(PointerEventData eventData)
    {
        //  画刷状态
        if (!string.IsNullOrEmpty(ZoneManager.Instance.floor))
        {
            ZoneManager.Instance._InputFloorComponent.OnPointerUp(eventData);

            return;
        }

        //if (ZoneUI._clickOB != null)
        //{
        //    _SelectObj = ZoneUI._clickOB._Object;

        //}

        //if (_SelectObj != null)
        {
            ZoneManager.Instance.Cancel(_SelectObj);
            _SelectObj = null;
        }
    }

    public virtual void OnPointerClick(PointerEventData eventData)
    {
    }

    public virtual void OnBeginDrag(PointerEventData eventData)
    {
    }

    public virtual void OnDrag(PointerEventData eventData)
    {
        //  画刷状态
        if (!string.IsNullOrEmpty(ZoneManager.Instance.floor))
        {
            ZoneManager.Instance._InputFloorComponent.OnDrag(eventData);

            return;
        }

        //if (_SelectObj != null)
        {
            ZoneManager.Instance.Move(eventData.delta);
        }
    }

    public virtual void OnEndDrag(PointerEventData eventData)
    {
    }

    public virtual void OnDrop(PointerEventData eventData)
    {
    }

    public virtual void OnScroll(PointerEventData eventData)
    {
    }

    GameObject  GetTarget (RaycastHit2D[] hits)
    {
        if (hits == null)
            return null;

        Vector2 cursorWorld = _Camera.ScreenToWorldPoint(Input.mousePosition);

        for (int i = 0; i <hits.Length; ++i)
        {
            RaycastHit2D h2d = hits[i];

            if (h2d.collider.gameObject.name == "EventSystem")
                continue;

            if (!ZoneManager.Instance.IsSelect(h2d.collider.gameObject))
                continue;

            Debug.Log(h2d.collider.gameObject.name);

            SpriteRenderer sprRend = h2d.collider.gameObject.GetComponent<SpriteRenderer>();
            if (sprRend.sprite == null)
                continue;

            Texture2D tex = (Texture2D)sprRend.sprite.texture;

            Vector2 pixelUV = h2d.point;

            int uvX = (int)((pixelUV.x - h2d.collider.gameObject.transform.position.x) / h2d.collider.bounds.size.x * tex.width);
            int uvY = (int)((pixelUV.y - h2d.collider.gameObject.transform.position.y) / h2d.collider.bounds.size.y * tex.height);

            Color hitColor = tex.GetPixel(uvX, uvY);

            if (hitColor.a == 0)
                continue;

            return h2d.collider.gameObject;
        }

        return null;
    }
}
